Data Types

Course- C IN LINUX >

This process is deeply philosophical; we make ontological assertions that this or that thing exists and we make epistemological assertions when we select particular data types or collections of data types to use to describe the attributes of these things. Heavy stuf with a great responsibility and not to be lightly undertaken.

As a practical example we might declare something that looks like the beginnings of a database record for geography.

#include<stdio.h>
#DEFINE STRINGSIZE 256

int main(int argc, char *argv[])
{

char town[STRINGSIZE]="Guildford";
char county[STRINGSIZE]="India";
char country[STRINGSIZE]="Great Britain";
int population=66773;
float latitude=51.254599;
float longitude=-0.566257;
printf("Town name: %s population:%d\n, town, population);
printf("County:%s\n", county);
printf("Country:%s\n", country);
printf("Locaiton laitude: %f longitude:%f\n",latitude, longitude);
printf("char=%d byte int=%d bytes float=%d bytes\n", sizeof(char),sizeof(int),sizeof(float));
printf("memory used%d bytes\n",((STRINGSIZE*3)*sizeof(char))
+sizeof(int)+(2*sizeof(float)));

return 0;

}                    
                    

Here we are doing the following:

  • asserting that all the character strings we will ever encounter in this application will be 255 limited to characters so we deine this with a preprocessor statement – these start with #.
  • assert that towns are associated with counties, and counties are associated with countries some hierarchical manner.
  • assert that the population is counted in whole numbers – no half-people.
  • assert the location is to be recorded in a particular variant (WGS84) of the convention of describing spots on the surface of the world in latitude and longitude that uses a decimal fraction for degrees, minutes, and seconds.

Each of these statements allocates memory within the scope of the function in which it is declared. Each data declaration will occupy an amount of memory in bytes and give that bit of memory a label which is the variable name. Each data type has a speciied size and the sizeof() library function will return this as an integer. In this case 3 × 256 characters, one integer, and two loats. he exact size is machine dependent but probably it is 780 bytes.

Outside the function in which the data has been declared this data is inaccessible – this is the scope of declaration. If we had declared outside the main() function it would be global in scope and other functions could access it. C lets you do this kind of dangerous stuf if you want to, so be careful.

Generally we keep a close eye on the scope of data, and pass either read-only copies, or labelled memory addresses to our data to parts of the programs that might need to do work on it and even change it. hese labelled memory addresses are called pointers.

We are using for output the printf family of library functions (sprintf for creating strings, fprintf for writing to iles etc.) which all use a common format string argument to specify how the data is to be represented.

  • %c character
  • %s string
  • %d integer
  • %f loating point number etc.

he remaining series of variables in the arguments are placed in sequence into the format string as speciied.

In C it is a good idea to intialise any data you declare as the contents of the memory allocated for them is not cleared but may contain any old rubbish.